// src/api/server.js
import { createServer, Model, Factory } from 'miragejs';
import faker from 'faker';

faker.locale = 'zh_CN';

export function makeServer({ environment = 'development' } = {}) {
  return createServer({
    environment,

    models: {
      product: Model,
      order: Model,
      user: Model,
      setting: Model,
    },

    factories: {
      product: Factory.extend({
        name() {
          return faker.commerce.productName();
        },
        category() {
          return faker.helpers.randomize(['electronics', 'clothing', 'books', 'food']);
        },
        price() {
          return parseFloat(faker.commerce.price());
        },
        stock() {
          return faker.datatype.number({ min: 0, max: 1000 });
        },
        image() {
          return faker.image.imageUrl(200, 200, 'products');
        },
        createdAt() {
          return faker.date.past();
        },
      }),

      order: Factory.extend({
        id() {
          return faker.datatype.uuid();
        },
        user() {
          return faker.name.findName();
        },
        amount() {
          return parseFloat(faker.commerce.price(100, 1000));
        },
        status() {
          return faker.helpers.randomize(['pending', 'paid', 'shipped', 'completed', 'cancelled']);
        },
        date() {
          return faker.date.recent().toISOString().split('T')[0];
        },
      }),

      user: Factory.extend({
        name() {
          return faker.name.findName();
        },
        email() {
          return faker.internet.email();
        },
        role() {
          return faker.helpers.randomize(['customer', 'admin']);
        },
        createdAt() {
          return faker.date.past();
        },
      }),
    },

    seeds(server) {
      server.createList('product', 20);
      server.createList('order', 50);
      server.createList('user', 30);
      // 初始化设置数据
      server.create('setting', {
        siteName: '商城管理系统',
        siteDescription: '专业的电商后台管理平台',
        siteUrl: 'https://example.com',
        logo: 'https://picsum.photos/200/80',
        smtpHost: 'smtp.example.com',
        smtpPort: 465,
        smtpUser: 'admin@example.com',
        smtpPass: 'password',
        smtpSecure: true,
        primaryColor: '#165DFF',
        themeMode: 'light',
        layoutMode: 'side',
        fixedHeader: true,
        fixedSidebar: true,
      });
    },

    routes() {
      this.namespace = 'api';

      // 产品API
      this.get('/products', (schema) => {
        return schema.products.all();
      });

      this.post('/products', (schema, request) => {
        const attrs = JSON.parse(request.requestBody);
        return schema.products.create(attrs);
      });

      this.put('/products/:id', (schema, request) => {
        const id = request.params.id;
        const attrs = JSON.parse(request.requestBody);
        return schema.products.find(id).update(attrs);
      });

      this.delete('/products/:id', (schema, request) => {
        const id = request.params.id;
        return schema.products.find(id).destroy();
      });

      // 订单API
      this.get('/orders', (schema) => {
        return schema.orders.all();
      });

      this.get('/orders/recent', (schema, request) => {
        const { limit = 10 } = request.queryParams;
        return schema.orders.all().slice(0, parseInt(limit));
      });

      // 用户API
      this.get('/users', (schema) => {
        return schema.users.all();
      });

      // 仪表盘数据
      this.get('/dashboard/stats', () => {
        return {
          todaySales: 2589.45,
          orderCount: 32,
          userCount: 1560,
          conversionRate: 3.25,
        };
      });

      this.get('/settings', (schema) => {
        console.log(schema.settings.first())
        return schema.settings.first();
      });
      
      this.put('/settings', (schema, request) => {
        const attrs = JSON.parse(request.requestBody);
        return schema.settings.first().update(attrs);
      });
    },
  });
}